package com.crape.account.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.crape.account.dao.IAccountDAO;
import com.crape.common.exceptions.DAOException;
import com.crape.common.ibatis.SqlMapClientDaoSupportEx;
import com.crape.common.model.Account;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.AcctRef;

public class AccountDAOImpl extends SqlMapClientDaoSupportEx implements IAccountDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> searchAccounts(String searchValue, Integer startIndex, Integer pageSize) throws DAOException {
		if (startIndex == null || pageSize == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("searchValue", searchValue);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("searchAccounts", params);
	}

	@Override
	public Long countSearchAccounts(String searchValue) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("searchValue", searchValue);
		return (Long) this.queryObject("countSearchAccounts", params);
	}

	@Override
	public void insertAccount(Account account) throws DAOException {
		if (account == null) {
			return;
		}
		this.insert("insertAccount", account);
	}

	@Override
	public void updateAccountUuid(Long accountid, String uuid) throws DAOException {
		Account account = new Account();
		account.setId(accountid);
		account.setUuid(uuid);
		this.update("updateAccountUuid", account);
	}

	@Override
	public void updateAccountStatus(Long accountid, Integer status) throws DAOException {
		if (accountid == null || status == null) {
			return;
		}
		Account account = new Account();
		account.setId(accountid);
		account.setStatus(status);
		this.update("updateAccountStatus", account);
	}

	@Override
	public void updateDarrenStatus(Long id, Integer usertype, Integer darrenstatus) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("usertype", usertype);
		params.put("darrenstatus", darrenstatus);
		this.update("updateDarrenStatus", params);
	}

	@Override
	public void deleteAccountById(Long id) throws DAOException {
		if (id == null) {
			return;
		}
		this.delete("deleteAccountById", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> listAccounts(Account account) throws DAOException {
		if (account == null) {
			return null;
		}
		return (List<Account>) this.queryList("listAccounts", account);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> findAccounts(Account account) throws DAOException {
		if (account == null) {
			return null;
		}
		return (List<Account>) this.queryList("findAccounts", account);
	}

	@Override
	public void updatePassword(Long id, String password) throws DAOException {
		if (id == null || password == null) {
			return;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("password", password);
		this.update("updatePassword", params);
	}

	@Override
	public void updateNickname(Long id, String nickname) throws DAOException {
		if (id == null || nickname == null) {
			return;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("nickname", nickname);
		this.update("updateNickname", params);
	}

	@Override
	public Boolean isAccountInfoExist(Long accountid) throws DAOException {
		if (accountid == null) {
			return null;
		}
		Integer num = (Integer) this.queryObject("isAccountInfoExist", accountid);
		if (num == null || num == 0) {
			return Boolean.FALSE;
		} else {
			return Boolean.TRUE;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getAccountInfos(AccountInfo accountInfo) throws DAOException {
		if (accountInfo == null) {
			return null;
		}
		return (List<AccountInfo>) this.queryList("getAccountInfos", accountInfo);
	}

	@Override
	public Long countAllDarrens() throws DAOException {
		return (Long) this.queryObject("countAllDarrens", null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getAllDarrens(Integer startIndex, Integer pageSize) throws DAOException {
		if (startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Integer> params = new HashMap<String, Integer>();
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("getAllDarrens", params);
	}

	@Override
	public Long countDarrens(Long accountid, String email) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("email", email);
		return (Long) this.queryObject("countDarrens", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getDarrens(Long accountid, String email, Integer startIndex, Integer pageSize) throws DAOException {
		if (startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("email", email);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("getDarrens", params);
	}

	@Override
	public void insertAccountInfo(AccountInfo accountInfo) throws DAOException {
		if (accountInfo == null) {
			return;
		}
		this.insert("insertAccountInfo", accountInfo);
	}

	@Override
	public void updateAccountInfo(AccountInfo accountInfo) throws DAOException {
		if (accountInfo == null) {
			return;
		}
		this.update("updateAccountInfo", accountInfo);
	}

	@Override
	public Long updateAccountAttentionCount(Long fansid) throws DAOException {
		if (fansid == null) {
			throw new IllegalArgumentException("accountid is null");
		}
		Long count = this.countAllUserAttentions(fansid);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("fansid", fansid);
		params.put("count", count);
		this.update("updateAccountAttentionCount", params);
		return count;
	}

	@Override
	public Long updateAccountFansCount(Long accountid) throws DAOException {
		if (accountid == null) {
			throw new IllegalArgumentException("accountid is null");
		}
		Long count = this.countAllUserFans(accountid);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("count", count);
		this.update("updateAccountFansCount", params);
		return count;
	}

	@Override
	public Long updateAccountArticleCount(Long accountid) throws DAOException {
		if (accountid == null) {
			throw new IllegalArgumentException("accountid is null");
		}
		Long count = this.countAllUserArticles(accountid);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("count", count);
		this.update("updateAccountArticleCount", params);
		return count;
	}

	@Override
	public Long updateAccountQuestionCount(Long accountid) throws DAOException {
		if (accountid == null) {
			throw new IllegalArgumentException("accountid is null");
		}
		Long count = this.countAllUserQuestions(accountid);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("count", count);
		this.update("updateAccountQuestionCount", params);
		return count;
	}

	@Override
	public Long countAllUserAttentions(Long accountid) throws DAOException {
		if (accountid == null) {
			throw new IllegalArgumentException("accountid is null");
		}
		return (Long) this.queryObject("countAllUserAttentions", accountid);
	}

	@Override
	public Long countAllUserArticles(Long accountid) throws DAOException {
		if (accountid == null) {
			throw new IllegalArgumentException("accountid is null");
		}
		return (Long) this.queryObject("countAllUserArticles", accountid);
	}

	@Override
	public Long countAllUserQuestions(Long accountid) throws DAOException {
		if (accountid == null) {
			throw new IllegalArgumentException("accountid is null");
		}
		return (Long) this.queryObject("countAllUserQuestions", accountid);
	}

	@Override
	public void insertAcctRef(AcctRef acctRef) throws DAOException {
		if (acctRef == null) {
			return;
		}
		this.insert("insertAcctRef", acctRef);
	}

	@Override
	public void deleteAcctRef(AcctRef acctRef) throws DAOException {
		if (acctRef == null) {
			return;
		}
		this.delete("deleteAcctRef", acctRef);
	}

	@Override
	public void backupAcctRef(AcctRef acctRef) throws DAOException {
		if (acctRef == null) {
			return;
		}
		this.insert("backupAcctRef", acctRef);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getTop8UserAttentions(Long accountid) throws DAOException {
		if (accountid == null) {
			return null;
		}
		return (List<AccountInfo>) this.queryList("getTop8UserAttentions", accountid);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getUserAttentionsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws DAOException {
		if (accountid == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("getUserAttentionsByAccountid", params);
	}

	@Override
	public Long countAllUserFans(Long accountid) throws DAOException {
		if (accountid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countAllUserFans", accountid);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getUserFansByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws DAOException {
		if (accountid == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("getUserFansByAccountid", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getUserVisitorsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws DAOException {
		if (accountid == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("getUserVisitorsByAccountid", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountInfo> getTop16SubjectVisitors(Long subjectid) throws DAOException {
		if (subjectid == null) {
			return null;
		}
		return (List<AccountInfo>) this.queryList("getTop16SubjectVisitors", subjectid);
	}

	@Override
	public List<AccountInfo> getPagedAccountByFaq(int pagesize) throws DAOException {
		return (List<AccountInfo>) this.queryList("getPagedAccountByFaq", pagesize);
	}

	@Override
	public List<AccountInfo> getPagedAccountByFaqRef(Long questionid, Integer startIndex, Integer pageSize) throws DAOException {
		if (questionid == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("questionid", questionid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("getPagedAccountByFaqRef", params);
	}

	@Override
	public List<AccountInfo> getPagedAccountByAnswerRef(Long answerid, Integer type, Integer startIndex, Integer pageSize) throws DAOException {
		if (answerid == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("answerid", answerid);
		params.put("type", type);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<AccountInfo>) this.queryList("getPagedAccountByAnswerRef", params);
	}

}
